<?php
class CustomerAction extends AdminBaseAction {

	public function index() {
		import('ORG.Util.Page');       //导入分页类
        $product   =   M('Customer');
		$condition['disabled']='false';
        $count  = $product->where($condition)->count();    //计算总数
        $Page = new Page($count, C('PAGE_NUM'));
        $list   = $product->limit($Page->firstRow. ',' . $Page->listRows)->where($condition)->order('id desc')->select();
        $Page->setConfig('header', '条数据');
        $Page->setConfig('first', '<<');
        $Page->setConfig('last', '>>');
        $page = $Page->show();
		$this->assign("count",$count);
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->display();
	}
	
	public function showadd(){
		$this->display();
	}

	public function add(){
		$product=M('Customer');
		$result=$product->add($_POST);
		
		if($result){
			$this->success('添加成功');
			$this->redirect('Customer/index');
		}else{
			$this->error('添加错误');
		}
	}

	public function recycle(){
		$map['id']  = array('in',$_POST['values']);
		$product=M('Customer');
		$data['disabled']='true';
		$product->where($map)->save($data);
		$this->redirect('Customer/index');
	}

	public function edit(){
		$id=$_GET['id'];
		$customer=M('Customer');
		$list=$customer->where(array('id'=>$id))->select();
		$vo=current($list);
		$this->assign('vo',$vo);
		$this->display();
	}

	public function save(){
		$id=$_POST['id'];
		$data['name']=$_POST['name'];
		$data['manager']=$_POST['manager'];
		$data['email']=$_POST['email'];
		$data['addr']=$_POST['addr'];
		$data['tel']=$_POST['tel'];
		$map['id']=array('neq',$id);
		$map['name']=array('eq',$data['name']);
		//$map['_complex'] = $where;
		$customer=M('Customer');
		$r=$customer->where($map)->select();
		if($r){
			$this->error('已经存在此客户名称');
		}else{
			$customer->where(array('id'=>$id))->save($data);
			$this->redirect('Customer/edit',array('id'=>$id),1,'编辑成功');
		}
	}
}
?>